"""
数据类型定义 - 用于镜像更新系统的类型安全
"""

from dataclasses import dataclass
from enum import Enum
from typing import Optional, Dict, Any, List
from datetime import datetime


class ContainerStatus(Enum):
    """容器状态枚举"""
    RUNNING = "running"
    STOPPED = "stopped"
    NOT_EXISTS = "not_exists"
    UNKNOWN = "unknown"


class ActionType(Enum):
    """操作类型枚举"""
    NONE = "none"          # 无需操作
    RESTART = "restart"    # 重启容器
    UPDATE = "update"      # 更新镜像并重建
    REBUILD = "rebuild"    # 完整重建
    CREATE = "create"      # 创建新容器


class StepType(Enum):
    """步骤类型枚举"""
    LWX_PARSE = "lwx_parse"
    IMAGE_VERIFY = "image_verify"
    CONDITIONAL_PULL = "conditional_pull"
    CONTAINER_REBUILD = "container_rebuild"


@dataclass
class ImageInfo:
    """镜像信息"""
    id: str
    repository: str
    tag: str
    created_at: str
    size: str
    digest: Optional[str] = None


@dataclass
class ContainerInfo:
    """容器信息"""
    id: str
    name: str
    image: str
    status: ContainerStatus
    created_at: str
    ports: Dict[str, str]


@dataclass
class VerificationResult:
    """校验结果"""
    needs_action: bool
    action_type: ActionType
    reason: str
    local_image_info: Optional[ImageInfo] = None
    container_info: Optional[ContainerInfo] = None


@dataclass
class OperationResult:
    """操作结果"""
    success: bool
    message: str
    details: Optional[Dict[str, Any]] = None
    error: Optional[str] = None


@dataclass
class WorkflowResult:
    """工作流结果"""
    success: bool
    action_taken: ActionType
    message: str
    steps_completed: int
    total_steps: int
    details: Dict[str, Any]
    execution_time: float


@dataclass
class StepContext:
    """步骤执行上下文"""
    school_name: str
    base_image: str
    full_image_url: str
    container_name: str
    compose_file: str
    verification_result: Optional[VerificationResult] = None


@dataclass
class StepResult:
    """步骤执行结果"""
    step_type: StepType
    success: bool
    message: str
    data: Optional[Dict[str, Any]] = None
    error: Optional[str] = None
    skipped: bool = False


@dataclass
class ContainerConfig:
    """容器配置"""
    image_url: str
    container_name: str
    school_name: str
    ports: Dict[str, str]
    environment: Dict[str, str]
    volumes: Optional[Dict[str, str]] = None


@dataclass
class SchoolConfig:
    """学校配置"""
    school_id: str
    school_name: str
    base_image: str